# 
# Download Control code is copyright Kumar McMillan and contributors.
# It is dual licensed under:
#   Gnu Public License Version 3
#   Limited Commercial License
#
# See the following wiki page for details:
# http://code.google.com/p/downloadcontrol/wiki/AboutTheLicense
#

from appengine_django.models import BaseModel
from google.appengine.ext import db

# Create your models here.

class AWSConfig(db.Model):
    access_key_id = db.StringProperty(required=True)
    secret_access_key = db.StringProperty(required=True)
    
    @classmethod
    def instance(cls):
        rs = cls.all()
        count = rs.count(2)
        if count == 1:
            return rs[0]
        elif count == 0:
            raise EnvironmentError("AWSConfig does not have any values")
        elif count == 2:
            raise EnvironmentError("AWSConfig has more values than expected")

class DownloadAccess(db.Model):
    is_valid = db.BooleanProperty(default=True)
    access_code = db.StringProperty(required=True)
    # This will be a key in the default bucket, 
    # i.e. path to file like /press-kits/something.zip
    s3_key = db.StringProperty(required=True)
    # number of times an authenticated key/URL was requested
    num_requests = db.IntegerProperty(default=0)